<style lang="less">
    @import './index.less';
</style>

<template>
    <div class="login">
        <div class="login-con">
            <Card icon="log-in" title="欢迎登录" :bordered="false">
                <div class="form-con">
                    <login-form @on-success-valid="handleSubmit"></login-form>
                    <p class="login-tip">输入任意用户名和密码即可</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import LoginForm from '_c/login-form'
import { mapActions } from 'vuex'
import { getCityData } from '@/api/city'

export default {
  components: {
    LoginForm
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'setCityData',
      'setAccessApiData',
      'setCityOriginData',
      'setLoginAccount'
    ]),
    handleSubmit ({ userName, password }) {
      this.handleLogin({ userName, password }).then(res => {
        if (res.r !== false) {
          this.getCityDataAction()
          this.setLoginAccount(userName)
          this.setAccessApiData(res.permit)
          this.$router.push({
            name: this.$config.homeName
          })
        } else {
          this.$Message.error(res.err)
        }
      })
    },
    getCityDataAction () {
      getCityData({}).then(ret => {
        let newData = ret.data.results.map((item, index) => {
          return {
            id: item.id,
            value: item.code,
            pcode: item.pcode,
            label: item.name
          }
        })
        this.setCityOriginData(newData)
        this.setCityData(this.$common.toTree(newData, 'children', 'value', 'pcode'))
      })
    }
  }
}
</script>
